package team.seekers.catering.community.service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import team.seekers.catering.community.entity.dao.Article;
import team.seekers.catering.community.entity.dao.FirstComment;
import team.seekers.catering.community.entity.dao.Post;
import team.seekers.catering.community.entity.dao.SecondComment;
import team.seekers.catering.community.entity.dto.add.PostAddDTO;
import team.seekers.catering.community.entity.dto.update.LikeOrCommentUpdateDTO;
import team.seekers.catering.community.entity.dto.update.PostUpdateDTO;
import team.seekers.catering.community.entity.vo.info.PostInfoVO;
import team.seekers.catering.community.mapper.PostMapper;
import team.seekers.catering.community.entity.vo.list.FirstCommentListVO;
import team.seekers.catering.community.entity.vo.list.PostListVO;
import team.seekers.catering.community.entity.vo.list.SecondCommentListVO;
import team.seekers.catering.community.mapper.PostMapper;
import team.seekers.catering.user.service.UserService;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class PostService extends ServiceImpl<PostMapper, Post> {

    @Autowired
    private UserService userService;

    @Autowired
    private FirstCommentService firstCommentService;

    @Autowired
    private SecondCommentService secondCommentService;

    /**
     * 增加一个帖子
     */
    public String add(PostAddDTO postAddDTO){
        Post post = new Post();
        BeanUtils.copyProperties(postAddDTO,post);
        post.setPicture(JSON.toJSONString(postAddDTO.getPicture()));
        save(post);
        return post.getId();
    }

    /**
     * 删除一个帖子
     */
    public Boolean delete(String id){
        boolean remove = removeById(id);
        return remove;
    }

    /**
     * 修改一个帖子(题目和内容)
     */
    public String updateMain(PostUpdateDTO postUpdateDTO){
        Post post = new Post();
        BeanUtils.copyProperties(postUpdateDTO,post);
        post.setPicture(JSON.toJSONString(postUpdateDTO.getPicture()));
        updateById(post);
        return post.getId();
    }

    /**
     * 修改一个帖子(点赞和评论)
     */
    public String updateForLikeOrComment(LikeOrCommentUpdateDTO likeOrCommentUpdateDTO){
        Post post = new Post();
        BeanUtils.copyProperties(likeOrCommentUpdateDTO,post);
        post.setCommentsFirst(JSON.toJSONString(likeOrCommentUpdateDTO.getCommentsFirst()));
        post.setLikeUser(JSON.toJSONString(likeOrCommentUpdateDTO.getLikeUser()));
        updateById(post);
        return post.getId();
    }

    /**
     * 根据id查询一个帖子
     */
    public PostInfoVO get(String id){
        Post post = getById(id);
        PostInfoVO postInfoVO = new PostInfoVO();
        BeanUtils.copyProperties(post,postInfoVO);
        postInfoVO.setUserName(userService.getById(post.getUserId()).getName());
        postInfoVO.setAvatar(userService.getById(post.getUserId()).getAvatar());
        List<String> userIds = JSONObject.parseArray(post.getLikeUser(), String.class);
        List<String> userNames = new ArrayList<>();
        for(String userId : userIds){
            userNames.add(userService.getById(userId).getName());
        }
        postInfoVO.setLikeUsersName(userNames);
        List<String> firstCommentIds = JSONObject.parseArray(post.getCommentsFirst(), String.class);
        List<FirstCommentListVO> firstCommentListVOS = new ArrayList<>();
        for(String firstCommentId : firstCommentIds){

            FirstCommentListVO firstCommentListVO = new FirstCommentListVO();
            FirstComment firstComment = firstCommentService.getById(firstCommentId);
            firstCommentListVO.setContent(firstComment.getContent());
            firstCommentListVO.setUserName(userService.getById(firstComment.getUserId()).getName());
            firstCommentListVO.setAvatar(userService.getById(firstComment.getUserId()).getAvatar());
            firstCommentListVO.setUserId(firstComment.getUserId());

            List<SecondCommentListVO> secondCommentListVOS = new ArrayList<>();
            List<SecondComment> secondComments = secondCommentService.queryAllCommentByFirst(firstCommentId);
            for(SecondComment secondComment : secondComments){
                SecondCommentListVO secondCommentListVO = new SecondCommentListVO();
                secondCommentListVO.setContent(secondComment.getContent());
                secondCommentListVO.setFromUserId(secondComment.getUserId());
                secondCommentListVO.setFromUserName(userService.getById(secondComment.getUserId()).getName());
                secondCommentListVO.setToUserId(secondComment.getToUserId());
                secondCommentListVO.setToUserName(userService.getById(secondComment.getToUserId()).getName());
                secondCommentListVOS.add(secondCommentListVO);
            }
            firstCommentListVO.setSecondCommentListVOS(secondCommentListVOS);

            firstCommentListVOS.add(firstCommentListVO);
        }
        postInfoVO.setCommentsFirst(firstCommentListVOS);
        return postInfoVO;
    }

    /**
     * 分页查询所有的帖子
     */
    public Map<String, Object> adminPagePost(long current, long limit, QueryWrapper<Post> wrapper) {
        Page<Post> page = new Page<>(current,limit);
        wrapper.orderByDesc("gmt_create");
        page(page,wrapper);
        Long total = page.getTotal();
        if(page.getRecords() == null){
            return null;
        }
        System.out.println("获取的记录是"+page.getRecords());

        List<PostListVO> list = new ArrayList<>();
        for (Post record : page.getRecords()) {
            PostListVO vo = new PostListVO();
            BeanUtils.copyProperties(record,vo);
            vo.setPicture(JSONObject.parseArray(record.getPicture(),String.class));
            vo.setUserName(userService.getById(record.getUserId()).getName());
            vo.setAvatar(userService.getById(record.getUserId()).getAvatar());
            list.add(vo);
        }
        Map<String,Object> map = new HashMap<>();
        map.put("total",total);
        map.put("rows",list);
        return map;
    }
}
